#version 150 core

uniform sampler2D diffuseTex;


uniform vec3 cameraPos;
uniform vec4 lightColour;
uniform vec3 lightPos;
uniform float lightRadius;

uniform int useTexture;

in Vertex
{
	vec4 colour;
	vec2 texCoord;
	vec3 normal;
	vec3 worldPos;
	vec2 reflectOffset;
} IN;

out vec4 gl_FragColor[2];

void main(void)
{
	
	vec4 diffuse = texture(diffuseTex, IN.texCoord);

	vec3 incident = normalize(lightPos - IN.worldPos);
	float lambert = max(0.0, dot(incident, IN.normal));	
	float dist = length(lightPos - IN.worldPos);
	float atten = 1.0 - clamp(dist / lightRadius, 0.0, 1.0);
	
	vec3 viewDir = normalize(cameraPos - IN.worldPos);
	vec3 halfDir = normalize(incident + viewDir);
	
	float rFactor = max(0.0, dot(halfDir,IN.normal));
	float sFactor = pow(rFactor, 33.f);
	
	vec3 colour = (diffuse.rgb * lightColour.rgb);
	colour += (lightColour.rgb * sFactor) * 0.33;
	gl_FragColor[0] = vec4(colour * atten * lambert, diffuse.a);
	gl_FragColor[0].rgb += (diffuse.rgb * lightColour.rgb) * 0.1;
	gl_FragColor[1] = vec4(IN.reflectOffset,0,1);
}